var express = require("express");
// 定义与前端交互的路由
var router = express.Router();
// 引入数据库连接
var connection = require("../db/sql.js");
var user = require("../db/userSql.js");
// 引入jsonwebtoken
let jwt = require("jsonwebtoken");

/* GET home page. */
router.get("/", function (req, res, next) {
  res.render("index", { title: "Express" });
});

//手机号密码登录
router.post("/api/login", function (req, res, next) {
  //接收用户传递的手机号和密码
  let params = {
    userTel: req.body.userTel,
    userPwd: req.body.userPwd,
  };
  //查询用户手机号
  connection.query(user.queryUserTel(params), function (error, results) {
    //手机号存在
    if (results.length > 0) {
      connection.query(user.queryUserPwd(params), function (err, result) {
        if (result.length > 0) {
          //手机号密码正确
          res.send({
            code: 200,
            data: {
              msg: "登录成功",
              success: true,
              data: result[0],
            },
          });
        } else {
          //密码不对
          res.send({
            code: 302,
            data: {
              msg: "密码不对",
              success: false,
            },
          });
        }
      });
    } else {
      //手机号不存在
      res.send({
        code: 301,
        data: {
          msg: "手机号不存在",
          success: false,
        },
      });
    }
  });
});

//注册
router.post("/api/register", function (req, res, next) {
  let params = {
    userTel: req.body.phone,
    userPwd: req.body.pwd,
  };
  //查询用户是否存在
  connection.query(user.queryUserTel(params), function (error, results) {
    if (error) throw error;
    //用户存在
    if (results.length > 0) {
      res.send({
        code: 200,
        data: {
          success: true,
          msg: "用户已存在，登录成功！",
          data: results[0],
        },
      });
    } else {
      //不存在，新增一条数据
      connection.query(user.inserData(params), function (err, result) {
        connection.query(user.queryUserTel(params), function (e, r) {
          res.send({
            code: 200,
            data: {
              success: true,
              msg: "注册成功！登录成功",
              data: r[0],
            },
          });
        });
      });
    }
  });
});

// 修改密码
router.post("/api/modify", function (req, res, next) {
  let params = {
    userTel: req.body.phone,
    userPwd: req.body.pwd,
  };
  //查询用户是否存在
  connection.query(user.queryUserTel(params), function (error, results) {
    //某一条记录数id
    let id = results[0].id;
    let pwd = results[0].pwd;
    // console.log(  `update user set pwd = replace(pwd,${pwd},${params.userPwd}) where id = ${id}` )
    connection.query(
      `update user set pwd = replace(pwd,'${pwd}','${params.userPwd}') where id = ${id}`,
      function (err, result) {
        res.send({
          code: 200,
          data: {
            success: true,
            msg: "修改成功",
          },
        });
      }
    );
  });
});

//首页数据
router.get("/api/index", function (req, res, next) {
  res.send({
    code: 0,
    data: {
      sortList: [
        {
          id: 1,
          title: "女装",
          imgUrl: "./images/1.webp",
        },
        {
          id: 2,
          title: "女鞋",
          imgUrl: "./images/2.webp",
        },
        {
          id: 3,
          title: "上衣",
          imgUrl: "./images/3.webp",
        },
        {
          id: 4,
          title: "美妆/护肤",
          imgUrl: "./images/4.webp",
        },
        {
          id: 5,
          title: "裤子",
          imgUrl: "./images/5.webp",
        },
        {
          id: 6,
          title: "母婴/童装",
          imgUrl: "./images/6.webp",
        },
        {
          id: 7,
          title: "裙子",
          imgUrl: "./images/7.webp",
        },
        {
          id: 8,
          title: "男装男鞋",
          imgUrl: "./images/8.webp",
        },
        {
          id: 9,
          title: "套装",
          imgUrl: "./images/9.webp",
        },
        {
          id: 10,
          title: "配饰",
          imgUrl: "./images/10.webp",
        },
      ],
      goodsList: [
        {
          id: 1,
          name: "60两件春秋圆环拉链上衣高领显瘦修身长袖打底衫T恤女针织毛衣",
          actor: "./images/list-1.webp",
          actorname: "小甜心",
          actortag: "爆单女王",
          price: "111",
          sales: 500,
          imgUrl: "./images/list-1.webp",
          leftUrl: "./images/zhibotongjia.png",
          rightUrl: "./images/zhibo.png",
          byUrl: "./images/baoyou.png",
        },
        {
          id: 2,
          name: "【送运费险】早秋黑色上衣女新款百搭纯色V领交叉长袖针织衫",
          actor: "./images/list-2.webp",
          actorname: "小甜心",
          actortag: "爆单女王",
          price: "222",
          sales: 222,
          imgUrl: "./images/list-2.webp",
          leftUrl: "./images/zhibotongjia.png",
          rightUrl: "./images/zhibo.png",
          byUrl: "./images/baoyou.png",
        },
        {
          id: 3,
          name: "2件60露背t恤女短袖新款夏修身针织衫上衣冰丝体恤微透打底衫",
          actor: "./images/list-3.webp",
          actorname: "小甜心",
          actortag: "爆单女王",
          price: "333",
          sales: 333,
          imgUrl: "./images/list-3.webp",
          leftUrl: "./images/zhibotongjia.png",
          rightUrl: "./images/zhibo.png",
          byUrl: "./images/baoyou.png",
        },
        {
          id: 4,
          name: "2件50元纯色百搭粉色体恤衫少女学生上衣服ins紧身短袖t恤",
          actor: "./images/list-4.webp",
          actorname: "小甜心",
          actortag: "爆单女王",
          price: "444",
          sales: 444,
          imgUrl: "./images/list-4.webp",
          leftUrl: "./images/zhibotongjia.png",
          rightUrl: "./images/zhibo.png",
          byUrl: "./images/baoyou.png",
        },
        {
          id: 5,
          name: "大码女装短款法式不规则高级感上衣女胖mm显瘦遮肉内搭打底衫",
          actor: "./images/list-5.jpg",
          actorname: "小甜心",
          actortag: "爆单女王",
          price: "555",
          sales: 555,
          imgUrl: "./images/list-5.jpg",
          leftUrl: "./images/zhibotongjia.png",
          rightUrl: "./images/zhibo.png",
          byUrl: "./images/baoyou.png",
        },
        {
          id: 6,
          name: "60两件长袖t恤女春秋新款网红百搭露肩宽松设计感打底心机上衣",
          actor: "./images/list-6.webp",
          actorname: "小甜心",
          actortag: "爆单女王",
          price: "666",
          sales: 666,
          imgUrl: "./images/list-6.webp",
          leftUrl: "./images/zhibotongjia.png",
          rightUrl: "./images/zhibo.png",
          byUrl: "./images/baoyou.png",
        },
      ],
    },
  });
});

// 通过数据库查询商品列表
router.get("/api/goods/shopList", function (req, res, next) {
  const searchName = req.query.searchName;
  console.log(searchName);
  connection.query(
    "SELECT * FROM goodslist WHERE name LIKE ?",
    ["%" + searchName + "%"],
    function (error, results) {
      if (error) {
        console.error("Database query error:", error);
        return res.status(500).send({
          code: 0,
          message: "服务器内部错误",
        });
      }
      res.send({
        code: 1,
        data: results,
      });
    }
  );
});

//分类的接口
router.get("/api/goods/list", function (req, res, next) {
  res.send({
    code: 0, //0表示成功，1表示失败
    data: [
      {
        //一级数据(左侧)
        id: 0,
        name: "正在流行",
        data: [
          {
            //二级数据(右侧)
            id: 0,
            name: "热销爆款",
            imgUrl: "/images/list-1.webp",
          },
          {
            id: 1,
            name: "短袖",
            imgUrl: "/images/list-2.webp",
          },
          {
            id: 2,
            name: "针织衫",
            imgUrl: "/images/list-3.webp",
          },
          {
            id: 3,
            name: "衬衫",
            imgUrl: "/images/list-4.webp",
          },
        ],
      },
      {
        //一级数据(左侧)
        id: 1,
        name: "上衣",
        data: [
          {
            //二级数据(右侧)
            id: 0,
            name: "热销爆款",
            imgUrl: "/images/list-4.webp",
          },
          {
            id: 1,
            name: "套装",
            imgUrl: "/images/list-3.webp",
          },
          {
            id: 2,
            name: "冲锋衣",
            imgUrl: "/images/list-2.webp",
          },
          {
            id: 3,
            name: "防晒衣",
            imgUrl: "/images/list-1.webp",
          },
        ],
      },
    ],
  });
});

// 通过商品id查询商品详情
router.get("/api/goods/id", function (req, res, next) {
  //前端传给后端的id
  let id = req.query.id;
  connection.query(
    "select * from goodslist where id=" + id + "",
    function (error, results) {
      if (error) throw error;
      res.json({
        code: 0,
        data: results[0],
      });
    }
  );
});

//添加购物车数据
router.post("/api/addCart", function (req, res, next) {
  //后端接收前端的参数
  let goodsId = req.body.goodsId;
  //token
  let token = req.headers.token;
  let tokenObj = jwt.decode(token);
  console.log(tokenObj);
  //查询用户
  connection.query(
    `select * from user where tel = ${tokenObj.tel}`,
    function (error, results) {
      //用户id
      let uId = results[0].id;
      //查询商品
      connection.query(
        `select * from goodslist where id=${goodsId}`,
        function (err, result) {
          let goodsName = result[0].name;
          let goodsPrice = result[0].price;
          let goodsImgUrl = result[0].imgUrl;
          connection.query(
            `insert into goodscart (uId,goods_id,goods_name,goods_price,goods_num,goods_imgUrl) values ("${uId}","${goodsId}","${goodsName}","${goodsPrice}","1","${goodsImgUrl}")`,
            function (e, r) {
              res.send({
                data: {
                  code: 200,
                  success: true,
                  msg: "添加成功",
                },
              });
            }
          );
        }
      );
    }
  );
});

//查询购物车数据
router.post("/api/selectCart", function (req, res, next) {
  //token
  let token = req.headers.token;
  let tokenObj = jwt.decode(token);
  //查询用户
  connection.query(
    `select * from user where tel = ${tokenObj.tel}`,
    function (error, results) {
      //用户id
      let uId = results[0].id;
      //查询购物车
      connection.query(
        `select * from goodscart where uId = ${uId}`,
        function (err, result) {
          res.send({
            data: {
              code: 200,
              success: true,
              data: result,
            },
          });
        }
      );
    }
  );
});

//修改购物车数量
router.post("/api/updateNum", function (req, res, next) {
  let id = req.body.id;
  let changeNum = req.body.num;

  connection.query(
    `select * from goodscart where id = ${id}`,
    function (error, results) {
      //原来的数量
      let num = results[0].goods_num;
      connection.query(
        `update goodscart set goods_num = replace(goods_num,${num},${changeNum}) where id = ${id}`,
        function (err, result) {
          res.send({
            data: {
              code: 200,
              success: true,
            },
          });
        }
      );
    }
  );
});

//生成一个订单
router.post("/api/addOrder", function (req, res, next) {
  //token
  let token = req.headers.token;
  let tokenObj = jwt.decode(token);
  //前端给后端的数据
  let goodsArr = req.body.arr;
  //生成订单号order_id，规则：时间戳 + 6为随机数
  function setTimeDateFmt(s) {
    return s < 10 ? "0" + s : s;
  }
  function randomNumber() {
    const now = new Date();
    let month = now.getMonth() + 1;
    let day = now.getDate();
    let hour = now.getHours();
    let minutes = now.getMinutes();
    let seconds = now.getSeconds();
    month = setTimeDateFmt(month);
    day = setTimeDateFmt(day);
    hour = setTimeDateFmt(hour);
    minutes = setTimeDateFmt(minutes);
    seconds = setTimeDateFmt(seconds);
    let orderCode =
      now.getFullYear().toString() +
      month.toString() +
      day +
      hour +
      minutes +
      seconds +
      Math.round(Math.random() * 1000000).toString();
    return orderCode;
  }
  /*
    未支付：1
    待支付：2
    支付成功：3
    支付失败：4 | 0
    */
  //商品列表名称
  let goodsName = [];
  //订单商品总金额
  let goodsPrice = 0;
  //订单商品总数量
  let goodsNum = 0;
  //订单号
  let orderId = randomNumber();

  goodsArr.forEach((v) => {
    goodsName.push(v.goods_name);
    goodsPrice += v.goods_price * v.goods_num;
    goodsNum += parseInt(v.goods_num);
  });
  //查询当前用户
  connection.query(
    `select * from user where tel = ${tokenObj.tel}`,
    function (error, results) {
      //用户id
      let uId = results[0].id;
      connection.query(
        `insert into store_order (order_id,goods_name,goods_price,goods_num,order_status,uId) values ('${orderId}','${goodsName}','${goodsPrice}','${goodsNum}','1',${uId})`,
        function (e, r) {
          connection.query(
            `select * from store_order where uId = ${uId} and order_id='${orderId}'`,
            function (err, result) {
              res.send({
                data: {
                  success: true,
                  code: 200,
                  data: result,
                },
              });
            }
          );
        }
      );
    }
  );
});

//查询订单
router.post("/api/selectOrder", function (req, res, next) {
  //接收前端给后端的订单号
  let orderId = req.body.orderId;
  connection.query(
    `select * from store_order where order_id='${orderId}'`,
    function (err, result) {
      res.send({
        data: {
          success: true,
          code: 200,
          data: result,
        },
      });
    }
  );
});

//修改订单状态
// 1.把订单状态从 “未支付（1）” 改成 “待支付（2）”
// 2.把用户购物车中选中的商品删掉（因为已经下单了，不需要留在购物车了）
router.post("/api/submitOrder", function (req, res, next) {
  //token
  let token = req.headers.token;
  let tokenObj = jwt.decode(token);
  //订单号
  let orderId = req.body.orderId;
  //购物车选中的商品id
  let shopArr = req.body.shopArr;
  //查询用户
  connection.query(
    `select * from user where tel = ${tokenObj.tel}`,
    function (error, results) {
      //用户id
      let uId = results[0].id;
      // 找到 “哪个” 订单（通过用户 id 和订单号查订单
      connection.query(
        `select * from store_order where uId = ${uId} and order_id = ${orderId}`,
        function (err, result) {
          //订单的数据库id
          let id = result[0].id;
          //修改订单状态 1==>2
          connection.query(
            `update store_order set order_status = replace(order_status,'1','2') where id = ${id}`,
            function (e, r) {
              //购物车数据删除
              shopArr.forEach((v) => {
                connection.query(
                  `delete from goodscart where id = ${v}`,
                  function () { });
              });
              res.send({
                data: {
                  code: 200,
                  success: true,
                },
              });
            }
          );
        }
      );
    }
  );
});


// 引入支付宝公钥和SDK（软件开发工具包），用于对接支付宝支付功能
// 这里的alipaySdk已经预先配置好了我们的支付宝商户信息
const alipaySdk = require("../db/aplipay.js"); 
// 引入支付宝表单处理工具，用于构建符合支付宝要求的支付请求参数
const AlipayFormData=require('alipay-sdk/lib/form').default

// 定义一个POST类型的接口，前端调用这个接口来发起支付
// 接口地址是/api/payment，前端通过这个地址发送支付请求
router.post('/api/payment',function(req,res,next){
    let orderId = req.body.orderId;
    let price = req.body.price;
    // 商品名称
    let name = req.body.name;
    
    // 初始化一个支付宝表单数据对象，用来组织支付需要的各种信息
    const formData = new AlipayFormData();
    
    // 设置请求方式为GET，这样支付宝会返回一个可以直接跳转的支付页面链接
    formData.setMethod('get');
    
    // 向表单中添加支付核心信息（业务内容）
    formData.addField('bizContent', {
      outTradeNo: orderId,// 我们系统的订单号，支付宝会用这个来关联交易
      productCode: 'FAST_INSTANT_TRADE_PAY',// 固定值，表示使用即时到账支付方式
      totalAmount: price,// 支付金额，单位是元
      subject: name,// 商品名称，会显示在支付页面上
    });
    
    // 设置支付完成后（不管成功还是失败）跳转的页面地址
    // 这里设置为我们自己前端的支付结果页面（http://localhost:5173/payment）
    formData.addField('returnUrl', 'http://localhost:5173/payment');
    
    // 调用支付宝SDK的exec方法，发起电脑网站支付请求
    // 'alipay.trade.page.pay'是支付宝提供的电脑网站支付接口名称
    // 传入配置好的formData，获取支付链接
    const result = alipaySdk.exec(
      'alipay.trade.page.pay',  // 支付宝接口名称，固定写法
      {},  // 额外参数，这里不需要，留空
      { formData: formData },  // 前面配置好的表单数据
    );
    
    // 当支付宝返回结果后，处理这个结果
    result.then(resp=>{
        // 把支付宝返回的支付链接发送给前端
        res.send({
            data:{
                code:200,          // 状态码，200表示成功
                success:true,      // 标识请求成功
                msg:'支付中',      // 提示信息
                paymentUrl : resp  // 支付宝的支付链接，前端拿到后会跳转到这个链接进行支付
            }
        })
    })
})


module.exports = router;
